package com.pmsystem.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pmsystem.pojo.Administrator;
import com.pmsystem.pojo.Root;
import com.pmsystem.pojo.User;
import com.pmsystem.service.AdministratorService;
import com.pmsystem.service.RootService;
import com.pmsystem.service.UserService;
import com.pmsystem.util.BeanUtil;

@Controller
@RequestMapping("root")
public class RootController {
	
	@Autowired
	private RootService rootService;
	@Autowired
	private AdministratorService administratorService;
	@Autowired
	private UserService userService;
	
	/**
	 * 
	 * <p>Title: login</p>  
	 * <p>Description:root用户登陆 </p>  
	 * @author 泽强
	 * @param session
	 * @param root
	 * @return
	 */
	@RequestMapping(value = "login", method = RequestMethod.POST)
	public String login(HttpServletRequest request,String username,String password) {
		Root root = null;
		root = rootService.login(username,password);
		System.out.println(root);
		if (root != null) {
			request.getSession().setAttribute("root", root);
			return "root/index";
		}
		request.setAttribute("msg", "账号或密码错误");
		return "root/login";
	}

	
	/**
	 * 
	 * <p>Title: logout</p>  
	 * <p>Description:root用户退出 </p>  
	 * @author 泽强
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "logout", method = RequestMethod.GET)
	public String logout(HttpSession session) {
		session.invalidate();
		return "login";
	}

	/**
	 * 
	 * <p>Title: addAdministrator</p>
	 * <p>Description:  增加二级管理员</p> 
	 * @author 泽强
	 * @return
	 */
	@RequestMapping(value = "addAdministrator", method = RequestMethod.POST)
	@ResponseBody
	public Object addAdministrator(@RequestParam Map<String,Object> map) {
		System.out.println(map);
		Administrator admin =BeanUtil.parserToBean(BeanUtil.mapToMapWithoutNull(map),Administrator.class);
		int flag = administratorService.addAdministrator(admin);
		if(flag!=0) {
			return "success!";
		}
		return "error!";
	}
	/**
	 * 
	 * <p>Title: addAdministrator</p>
	 * <p>Description:  增加二级管理员</p> 
	 * @author 泽强  改-魏家洛
	 * @return
	 */
	@RequestMapping(value = "addAdministrator1", method = RequestMethod.GET)
	@ResponseBody
	public Object addAdministrator(Administrator admin) {
		int flag = administratorService.addAdministrator(admin);
		if(flag!=0) {
			return "success!";
		}
		return "error!";
	}
	/**
	 * 
	 * <p>Title: selectAdministrator</p>
	 * <p>Description:  按一定条件查找Admin信息，注意SQL</p> 
	 * @author 泽强
	 * @param map
	 * @param model
	 * @return  
	 */
	@RequestMapping(value = "selectAdministrator", method = RequestMethod.GET)
	@ResponseBody
	public Object selectAdministrator(@RequestParam Map<String, Object> map) {
		List<Administrator> list = administratorService.findAdmin(map);
		System.out.println(list);
		return list;
	}
	
	
	/**
	 * 
	 * <p>Title: deleteAdministrator</p>
	 * <p>Description: 根据id批量删除二级管理员 </p> 
	 * @author 泽强
	 * @param idList
	 * @return
	 */
//	@SuppressWarnings("unchecked")
//	@RequestMapping(value = "deleteAdministrator", method = RequestMethod.GET)
//	@ResponseBody
//	public String deleteAdministrator(Map<String, Object> map) {
//		List<String> idList=(List<String>) map.get("idList");
//		administratorService.deleteAdministrator(idList);
//		return "success";
//	}
	
	
	/**
	 * 
	 * <p>Title: updateAdministrator</p>
	 * <p>Description:  更新二级管理员信息</p> 
	 * @author 泽强
	 * @param administrator
	 * @return
	 */
	@RequestMapping(value = "updateAdministrator", method = RequestMethod.POST)
	@ResponseBody
	public String updateAdministrator(@RequestParam Map<String,Object> map) {
		Administrator admin =BeanUtil.parserToBean(BeanUtil.mapToMapWithoutNull(map),Administrator.class);
		boolean bool = administratorService.updateAdministrator(admin);
		if(bool) {
			return "success";
		}
		return "error";
	}
	
	/**
	 * 
	 * <p>Title: initDB</p >  
	 * <p>Description: 清空表</p >  
	 * @author 泽强
	 * @param map
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "initDB", method = RequestMethod.GET)
	public String initDB(@RequestParam Map<String,Object> map) {
//		List<String> list = new ArrayList<>();
//		list.add("administrator");
//		list.add("fix");
//		list.add("file");
//		for (String str : list) {
//			System.out.println(str);
//		}
//		map.put("list", list);
		String key = (String) map.get("key");
		if(key=="asd123") {
			List<String> list = (List<String>)map.get("list");
			rootService.initDB(list);
		}
		return "success";
	}
	
	/**
	 * 
	 * <p>Title: updateUser</p >  
	 * <p>Description: 更改用户信息</p >  
	 * @author 泽强
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "updateUser", method = RequestMethod.POST)
	public Object updateUser(@RequestParam Map<String,Object> map) {
		User user = BeanUtil.parserToBean(BeanUtil.mapToMapWithoutNull(map),User.class);
		boolean flag = userService.update(user);
		if(flag) {
			return "success";
		}
		return "error";
	}
	
	@RequestMapping(value = "showUser", method = RequestMethod.GET)
	@ResponseBody
	public Object showUser() {
		return userService.showUser();
	}
}
